﻿using System;
using System.Collections.Generic;
using Model.Interfaces;
using Model.ModelEventArgs;

namespace Model.NotifyingCollections
{
    public class NotifyingModelList : List<IModel>
    {
        public event EventHandler<ModelItemEventArgs> AddedItemEvent;
        public event EventHandler<ModelItemEventArgs> RemovedItemEvent;

        public new void Add(IModel item)
        {
            base.Add(item);
            if (AddedItemEvent != null)
                AddedItemEvent(this, new ModelItemEventArgs {ModelItem = item});
        }

        public new bool Remove(IModel item)
        {
            var result = base.Remove(item);
            if (result && RemovedItemEvent != null)
                RemovedItemEvent(this, new ModelItemEventArgs { ModelItem = item });
            return result;
        }

        public new void RemoveAt(int index)
        {
            var item = base[index];
            base.RemoveAt(index);
            if (RemovedItemEvent != null)
                RemovedItemEvent(this, new ModelItemEventArgs { ModelItem = item });
        }
    }
}
